import 'package:flutter/material.dart';

///
/// des: 混合动画
///
class MixedAnimationDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MixedAnimationDemo();
}

class _MixedAnimationDemo extends State<MixedAnimationDemo> with SingleTickerProviderStateMixin {
  AnimationController animationController; // 动画控制器
  var animationSize; // 动画大小
  var animationColor; // 动画颜色

  // 初始化
  @override
  void initState() {
    super.initState();
    animationController = AnimationController( // 创建动画控制器,并设置动画执行时间
      duration: Duration(seconds: 1),
      // vsync: this // vsync: this, // del by wcx TODO
    )
      ..addListener(() { // 添加监听
        setState(() {}); // 刷新UI
      })
      ..addStatusListener((status) { // 添加动画状态监听
        if (status == AnimationStatus.completed) {
          //执行结束反向执行
          animationController.reverse();
        } else if (status == AnimationStatus.dismissed) {
          //反向执行结束正向执行
          animationController.forward();
        }
      });

//    animationSize = Tween(begin: 80.0, end: 100.0);
//    animationColor = ColorTween(begin: Colors.red, end: Colors.black);

    animationSize = Tween(begin: 80.0, end: 100.0).animate(CurvedAnimation(parent: animationController, curve: Interval(0.0, 0.5)));
    animationColor = ColorTween(begin: Colors.red, end: Colors.black).animate(CurvedAnimation(parent: animationController, curve: Interval(0.5, 1.0)));
    animationController.forward();
  }

  /// UI构建
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
//        height: animationSize.evaluate(animationController),
//        width: animationSize.evaluate(animationController),
//        color: animationColor.evaluate(animationController),
        height: animationSize.value,
        width: animationSize.value,
        color: animationColor.value,
      ),
    );
  }
}
